#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>


#define SERVER_PORT 888


/* socket
 * connect
 * send/recv
 */
 
int main (int argc, char **argv)
{
	int iSocketClient;
	int iRet;
	struct sockaddr_in tSocketServerAddr;
	unsigned char ucSendBuff[1000];
	int iSendLen;

	if (argc != 2)
	{
		printf("Usage:\n");
		printf("%s <ip_server>\n", argv[0]);
		return -1;
	}
	
	iSocketClient = socket(AF_INET, SOCK_STREAM, 0);
	
	/* AF_INET Ipv4 */
	tSocketServerAddr.sin_family 		= AF_INET;
	tSocketServerAddr.sin_port	  		= htons (SERVER_PORT); /* host to net, short */
//	tSocketServerAddr.sin_addr.s_addr	= ;
	if (inet_aton(argv[1], &tSocketServerAddr.sin_addr) == 0)
	{
		printf("invaild server ip\0");
		return -1;
	}
	memset(tSocketServerAddr.sin_zero, 0, sizeof(tSocketServerAddr.sin_zero));

	iRet = connect(iSocketClient, (const struct sockaddr *)&tSocketServerAddr, sizeof(struct sockaddr));
	if (iRet == -1)
	{
		printf("connect error!\n");
		return -1;
	}

	while (1)
	{
		if (fgets(ucSendBuff, 999, stdin))
		{
			iSendLen = send(iSocketClient, ucSendBuff, strlen(ucSendBuff), 0);
			if (iSendLen <= 0)
			{
				close(iSocketClient);
				return -1;
			}
		}
	}
	close(iSocketClient);
	return 0;	
}
